package com.wn.controller;

import com.wn.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author yellow Docter
 * @date 2022 -10-24
 * @desc
 */
@RestController
@RequestMapping("consumer")
@Slf4j
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    public static final String URL = "http://localhost:9000/user";

    @PostMapping("save")
    public String  saveUser(@RequestBody User user){
        //远程调用 调用post 的方法 参数是:user
        // 返回结果是 字符串
        String result = restTemplate.postForObject(URL+"/save", user, String.class);
        return result;
    }

    @DeleteMapping("{id}")
    public String   deleteUser(@PathVariable("id")Integer id){

        //远程调用
        String url = URL + "/" + id;
        restTemplate.delete(url);
        log.info("delete success");

        return "delete success";
    }

    @PutMapping("update")
    public String  updateUser(@RequestBody User user){

        //远程调用
        restTemplate.put(URL+"/update",user);
        log.info("update success");

        return "update success";
    }

    @GetMapping("/{id}")
    public User  getUserById(@PathVariable("id")Integer id){

        //远程调用
        String url = URL + "/" + id;
        User user = restTemplate.getForObject(url, User.class);

        return user;
    }

}
